//
// OpenArrow Utility Library
// Copyright (c) 2006
// by OpenArrow Software ( http://www.openarrow.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Text;
using OpenArrow.Properties;

namespace OpenArrow.Reporting
{
    /// <summary>
    /// Utility class that implements <see cref="IDisposable"/> in order to allow developers to use the <see langword="using"/> statement when reporting progress
    /// <seealso cref="IProgressReporter"/>
    /// </summary>
    /// <remarks>
    /// Basically, this is just a simple class that calls <see cref="IProgressReporter.EndTask"/> when disposed. It allows for the usage of the
    /// <see langword="using"/> statement when calling <see cref="IProgressReporter.StartTask"/>.
    /// </remarks>
    public class ProgressReporterToken : IDisposable
    {
        #region Private Fields

        private IProgressReporter reporter; 

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Constructs a new <see cref="ProgressReporterToken"/> object.
        /// </summary>
        /// <param name="reporter">The <see cref="IProgressReporter"/> object that created this token</param>
        /// <exception cref="ArgumentNullException"><paramref name="reporter"/> is <see langword="null"/></exception>
        public ProgressReporterToken(IProgressReporter reporter)
        {
            if (reporter == null)
                throw new ArgumentNullException(String.Format(Resources.Exception_ArgIsNull, "reporter"), "reporter");

            this.reporter = reporter;
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Disposes of a <see cref="ProgressReporterToken"/> object, calling <see cref="IProgressReporter.EndTask"/> on the reporter specified
        /// in the <see cref="ProgressReporterToken(IProgressReporter)"/> constructor
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Disposes of a <see cref="ProgressReporterToken"/> object, calling <see cref="IProgressReporter.EndTask"/> on the reporter specified
        /// in the <see cref="ProgressReporterToken(IProgressReporter)"/> constructor
        /// </summary>
        /// <param name="disposing"><b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only unmanaged resources</param>
        protected virtual void Dispose(bool disposing)
        {
            if(disposing)
                reporter.EndTask();
        }

        #endregion
    }
}
